package ui.activity.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.profit.contract.ProfitContract;
import ui.adapter.SearchInfoAdapter;
import ui.model.ProfitBean;
import ui.model.ProfitNewBean;
import ui.model.SearchInfoBean;


/**
 * 首页搜索页面
 * Created by chenjian on 2017/8/4.
 */

public class SearchInfoAct extends BaseAct implements ProfitContract.View {


    @BindView(R.id.cancel_btn)
    TextView cancel_btn;//取消按钮


    @BindView(R.id.top_ss)
    EditText top_ss;//搜索内容输入框

    @BindView(R.id.searchinfo_list)
    RecyclerView searchinfo_list;//搜索内容输入框
    SearchInfoAdapter adapter;
    List<SearchInfoBean> questionInfo = new ArrayList<>();
    List<SearchInfoBean> questionInfoByEdit = new ArrayList<>();
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_searchinfo);
    }

    @Override
    protected void setup() {
        questionInfo.addAll(SearchInfoBean.setData());
        questionInfoByEdit.addAll(SearchInfoBean.setData());
        if(null == adapter){
            adapter = new SearchInfoAdapter(SearchInfoAct.this, questionInfoByEdit);
        }
        searchinfo_list.setLayoutManager(new LinearLayoutManager(SearchInfoAct.this));
        searchinfo_list.setItemAnimator(new DefaultItemAnimator());
//        searchinfo_list.addItemDecoration(new DividerItemDecoration(SearchInfoAct.this,1));
        searchinfo_list.setAdapter(adapter);
        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishAct();
            }
        });
        top_ss.setFocusable(true);
        top_ss.requestFocus();
        InputMethodManager inputManager =(InputMethodManager)top_ss.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(top_ss, 0);

        top_ss.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                questionInfoByEdit.clear();
                for (int i = 0;i<questionInfo.size();i++){
                    if(s.toString().equals("")||questionInfo.get(i).getContent().contains(s.toString())){
                        questionInfoByEdit.add(questionInfo.get(i));
                    }
                }
                adapter.setupData(questionInfoByEdit,s.toString());
            }
        });
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
//        CommonViewUtil.setDarw(right, this, R.mipmap.back, -1,
//                -1, -1);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void upDateUI(ProfitBean bean) {

    }

    @Override
    public void upDateTopUI(ProfitNewBean bean) {

    }
}
